package cn.hk.vaccination.service.impl;

import cn.hk.user.domain.UserInfo;
import cn.hk.user.mapper.UserInfoMapper;
import cn.hk.vaccination.domain.SubscribeDetail;
import cn.hk.vaccination.domain.SubscribeInfo;
import cn.hk.vaccination.domain.vo.CancelOderVo;
import cn.hk.vaccination.domain.vo.VaccinationVo;
import cn.hk.vaccination.service.*;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.*;

@Service
public class VaccinationServiceImpl implements VaccinationService {

    @Autowired
    private DeptVaccinesStockService deptVaccinesStockService;

    @Autowired
    private VaccinesDeptRegisterService vaccinesDeptRegisterService;

    @Autowired
    private VaccinesPlanCommonService vaccinesPlanCommonService;

    @Autowired
    private VaccinesPlanOldService vaccinesPlanOldService;

    @Autowired
    private VaccinesPlanService vaccinesPlanService;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private SubscribeDetailService subscribeDetailService;

    @Autowired
    private SubscribeInfoService subscribeInfoService;


    /**
     * 获取疫苗集合
     *
     * @param
     * @return
     */
    @Override
    public List<VaccinationVo> getVaccinationVoList(UserInfo curUser) {
        //空值处理
        if (BeanUtil.isEmpty(curUser) || ObjectUtil.hasEmpty(curUser.getBirthday()) || ObjectUtil.hasEmpty(curUser.getGender())) {
            return null;
        }
        //存放疫苗列表
        List<VaccinationVo> vaccinations = new ArrayList<>();
        try {
            //根据年龄 性别 获取筛选疫苗列表
            return getVaccinationVos(curUser, vaccinations);
        } catch (Exception e) {
            Console.error("发生了异常", e);
        }
        return null;
    }

    @Override
    public UserInfo getUserById(Long userId) {
        return userInfoMapper.selectOneById(userId);
    }

    @Override
    public Map<String, Object> getOderDateBYDeptId(Long deptId) {
        Map<String, Object> resultMap = new HashMap<>();
        List<SubscribeDetail> amSubscribeDetails = new ArrayList<>();
        List<SubscribeDetail> pmSubscribeDetails = new ArrayList<>();
        List<SubscribeDetail> subscribeDetailList = subscribeDetailService.selectListByDeptId(deptId);
        resultMap.put("subscribeDetailList", subscribeDetailList);

        return resultMap;
    }

    @Override
    public AjaxResult sinIn(SubscribeInfo subscribeInfo) {
        DateTime nowTime = DateUtil.date();
        String[] subTimeArr = subscribeInfo.getTimeSolt().split("-");
        String sunTimeStrStart = subscribeInfo.getSubscribeTime() + " " + subTimeArr[0] + ":00";
        String sunTimeStrEnd = subscribeInfo.getSubscribeTime() + " " + subTimeArr[1] + ":00";
        //现场签到时间大于预约时间
        if (DateUtil.compare(nowTime, DateUtil.parseDateTime(sunTimeStrEnd)) > 0) {
            return AjaxResult.error("预约时间已过,无法签到");
            //现场签到小于预约时间
        } else if (DateUtil.compare(nowTime, DateUtil.parseDateTime(sunTimeStrStart)) < 0) {
            return AjaxResult.error("预约时间未到,无法签到");
        }
        return subscribeInfoService.signIn(subscribeInfo.getSubscribeDetailId()) ? AjaxResult.success("签到成功") : AjaxResult.error("签到失败");
    }

    @Override
    @Transactional
    public AjaxResult cancelOder(CancelOderVo cancelOderVo) {

        Boolean r = subscribeInfoService.cancelOder(cancelOderVo.getSubscribeInfoId());
        if (r) {
            SubscribeDetail subscribeDetail = subscribeDetailService.getOneById(cancelOderVo.getSubscribeDetailId());
            SubscribeDetail subscribeDetailVo = null;
            //上午
            if ("1".equals(subscribeDetail.getApFlag())) {

                subscribeDetailVo = new SubscribeDetail();
                subscribeDetailVo.setId(subscribeDetail.getId());
                subscribeDetailVo.setSubValue(subscribeDetail.getSubValue() + 1);
                subscribeDetailVo.setSoltTime(subscribeDetail.getSoltTime());
                Boolean result = subscribeDetailService.editById(subscribeDetailVo);

                return result ? AjaxResult.success("取消成功") : AjaxResult.error("取消失败");

                //下午
            } else if ("2".equals(subscribeDetail.getApFlag())) {

                subscribeDetailVo = new SubscribeDetail();
                subscribeDetailVo.setId(subscribeDetail.getId());
                subscribeDetailVo.setSubValue(subscribeDetail.getSubValue() + 1);
                subscribeDetailVo.setSoltTime(subscribeDetail.getSoltTime());
                Boolean result = subscribeDetailService.editById(subscribeDetailVo);

                return result ? AjaxResult.success("取消成功") : AjaxResult.error("取消失败");
            }
        }

        return AjaxResult.error("取消失败");
    }

    @Override
    public List<VaccinationVo> getVaccinationVoListByDeptIdAndUser(UserInfo curUser, Long deptId) {
        //空值处理
        if (BeanUtil.isEmpty(curUser) || ObjectUtil.hasEmpty(curUser.getBirthday()) || ObjectUtil.hasEmpty(curUser.getGender())) {
            return null;
        }
        //存放疫苗列表
        List<VaccinationVo> vaccinations = new ArrayList<>();
        try {

            //根据年龄 性别 获取筛选疫苗列表
            return getVaccinationVosByDeptIdAndUser(curUser, deptId, vaccinations);

        } catch (Exception e) {
            Console.error("发生了异常", e);
        }
        return null;
    }

    private List<VaccinationVo> getVaccinationVosByDeptIdAndUser(UserInfo curUser, Long deptId, List<VaccinationVo> vaccinations) {
        Integer age = DateUtil.ageOfNow(curUser.getBirthday());
        if (age >= 7 && age <= 65) {
            //普通人
            List<VaccinationVo> vaccinationVoList = vaccinesPlanCommonService.getVaccinationVoListByCommon(age, curUser.getGender(), deptId);
            if (vaccinationVoList.size() > 0) {
                vaccinations.addAll(vaccinationVoList);
            }
            return vaccinations;
        } else if (age > 65) {
            //老年人
            List<VaccinationVo> vaccinationVoList = vaccinesPlanOldService.getVaccinationVoListByOld(deptId);
            if (vaccinationVoList.size() > 0) {
                vaccinations.addAll(vaccinationVoList);
            }
            return vaccinations;
        } else {
            // 婴儿
            //算出婴儿出生月份
            Long months = DateUtil.betweenMonth(curUser.getBirthday(), new Date(), false);
            Double monthValue = months.doubleValue();
            List<VaccinationVo> vaccinationVoList = vaccinesPlanService.getVaccinationVoListByChr(deptId, monthValue);
            if (vaccinationVoList.size() > 0) {
                vaccinations.addAll(vaccinationVoList);
            }
            return vaccinations;
        }
    }


    /**
     * 获取疫苗接种列表
     *
     * @param curUser
     * @param vaccinations
     * @param
     * @return
     */
    private List<VaccinationVo> getVaccinationVos(UserInfo curUser, List<VaccinationVo> vaccinations) {
        String birthday = DateUtil.formatDate(curUser.getBirthday());
        Integer age = DateUtil.ageOfNow(birthday);

        if (age >= 7 && age <= 65) {
            //普通人
            List<VaccinationVo> vaccinationVoList = vaccinesPlanCommonService.getVaccinationVoListByCommon(age, curUser.getGender(), curUser.getDeptId());
            if (vaccinationVoList.size() > 0) {
                vaccinations.addAll(vaccinationVoList);
            }
            return vaccinations;
        } else if (age > 65) {
            //老年人
            List<VaccinationVo> vaccinationVoList = vaccinesPlanOldService.getVaccinationVoListByOld(curUser.getDeptId());
            if (vaccinationVoList.size() > 0) {
                vaccinations.addAll(vaccinationVoList);
            }
            return vaccinations;
        } else {
            // 婴儿
            //算出婴儿出生月份
            Long months = DateUtil.betweenMonth(curUser.getBirthday(), new Date(), false);
            Double monthValue = months.doubleValue();
            List<VaccinationVo> vaccinationVoList = vaccinesPlanService.getVaccinationVoListByChr(curUser.getDeptId(), monthValue);
            if (vaccinationVoList.size() > 0) {
                vaccinations.addAll(vaccinationVoList);
            }
            return vaccinations;
        }
    }

    /**
     * 获取疫苗id的集合
     *
     * @param curUser
     * @return
     */
    private List<Long> getVaccinesIdList(UserInfo curUser) {
        List<Long> vaccinesIdsOne = deptVaccinesStockService.getVaccinesIdsByDeptId(curUser.getDeptId());
        //被取消的id集合
        List<Long> vaccinesIdsTwo = vaccinesDeptRegisterService.getVaccinesIdsByDeptId(curUser.getDeptId());
        //两个结合的差集,保留第一个有的,第二个没有的
        List<Long> resultList = CollUtil.subtractToList(vaccinesIdsOne, vaccinesIdsTwo);
//        Console.log(resultList);
        return resultList;
    }


}
